package dao.impl;

import bean.User;
import dao.BaseDao;
import dao.UserDao;

import java.util.ArrayList;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public ArrayList<User> queryAll() {
        String sql = "SELECT * FROM `t_user`";
        return queryForList(User.class, sql);
    }

    @Override
    public User queryByNameAndPassword(String name, String password) {
        String sql = "SELECT * FROM `t_user` WHERE `username`=? AND `password`=?";
        return queryForOne(User.class, sql, name, password);
    }

    @Override
    public int insert(User user) {
        String sql = "INSERT INTO `t_user` (`username`,`password`,`gender`,`email`,`telephone`,`introduce`,`activeCode`,`state`,`role`,`registeTime`)VALUES(?,?,?,?,?,?,?,?,?,?)";
        return update(sql, user.getUsername(), user.getPassword(), user.getGender(), user.getEmail(), user.getTelephone(), user.getIntroduce()
                , user.getActiveCode(), user.getState(), user.getRole(), user.getRegisteTime());
    }

    @Override
    public int update(User user) {
        String sql = "UPDATE `t_user` SET `username`=?,`password`=?,`gender`=?,`email`=?,`telephone`=?,`introduce`=?,`activeCode`=?,`state`=?,`role`=?,`registeTime`=? WHERE `id`=?";
        return update(sql, user.getUsername(), user.getPassword(), user.getGender(), user.getEmail(), user.getTelephone(),
                user.getIntroduce(), user.getActiveCode(), user.getState(), user.getRole(), user.getRegisteTime(),user.getId());
    }

    @Override
    public int delete(Integer id) {
        String sql = "DELETE FROM `t_user` WHERE `id`=?";
        return update(sql, id);
    }

    @Override
    public ArrayList<User> queryUserByUsername(String name) {
        String sql = "SELECT * FROM `t_user` WHERE `username`=?";
        return queryForList(User.class, sql, name);
    }

    @Override
    public User queryById(Integer id) {
        String sql = "SELECT * FROM `t_user` WHERE `id`=?";
        return queryForOne(User.class, sql, id);
    }
}
